import { where } from 'sequelize/types'
import { Remark } from '../model/record'
import { IRemarkModel } from '../model/record/remark'

export const create = async (remark: IRemarkModel) => {
    return Remark.create(remark)
}
export const bulkBuild = async (remarks: [IRemarkModel]) => {
    return Remark.bulkCreate(remarks)
}
export const update = async (id: number, payload: {}) => {
    return Remark.update(payload, {
        where: {
            id: id
        }
    })
}
export const findRemark = async (remark: IRemarkModel) => {
    const data = await Remark.findOne({
        where: {
            content: `${remark.content}`
        }
    })
    if (!data) {
        global.UnifyResponse.error({ code: -1, message: '评价不存在' })
    }
    return data!
}
export const getById = async (id: number) => {
    const remark = await Remark.findByPk(id)

    if (!remark) {
        global.UnifyResponse.error({ code: -1, message: '该评价不存在' })
    }
    return remark!
}
export const getByMould = async (id: number) => {
    const remark = await Remark.findAll({
        where: {
            mouldId: id
        },
        order: [[ 'createdAt', 'DESC' ]],
    })
    if (!remark) {
        global.UnifyResponse.error({ code: -1, message: '当前课程模板还没有成长记录' })
    }
    return remark!
}
export const deleteById = async (id: number) => {
    const numDeleted = await Remark.destroy({
        where: { id }
    })
    return numDeleted > 0
}

export const getAll = async () => {
    return Remark.findAll()
}
